<?php

namespace app\admin\controller\oa;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;
use think\Env;
use think\exception\ValidateException;

/**
 * 全部公告
 *
 * @icon fa fa-user
 */
class Notice extends Backend
{


    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $models = null;
    protected $categorylist = [];
    private $redis;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Notice;
        $this->redis = redis(Env::get('redis.host'), Env::get('redis.port'), Env::get('redis.pwd'), Env::get('redis.db'));

        $data = Db::table('fa_auth_group')
            ->field('id,name')
            ->where('pid',1)
            ->select();

        $this->view->assign("parentList", $data);
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order('id', 'desc')
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if ($params['type'][0] == ''){
                    $this->error(__('发送部门不能为空'));
                }

                //开启事务
                Db::startTrans();

                $insert = Db::table('fa_oa_notice')
                    ->insertGetId([
                        'title' => $params['title'],
                        'content' => $params['content'],
                        'create_time' => time(),
                    ]);
                $arrs = [];

                if ($params['type'][0] == 0){
                    $for = Db::table('fa_admin')
                        ->field('id')
                        ->select();

                    if (!empty($for)){
                        foreach ($for as $k=>$v){
                            $arrs[$k] = [
                                'admin_id' => $v['id'],
                                'notice_id' => $insert,
                                'type' => 1,
                                'create_time' => time(),
                            ];
                        }

                        $inserts = Db::table('fa_oa_relevance')
                            ->insertAll($arrs);
                    }else{
                        $inserts = 1;
                    }
                }else{
                    $ids = implode(',',$params['type']);

                    $for = Db::table('fa_auth_group_access')
                        ->where('group_id','in',$ids)
                        ->select();

                    if (!empty($for)){
                        foreach ($for as $k=>$v){
                            $arrs[$k] = [
                                'admin_id' => $v['uid'],
                                'notice_id' => $insert,
                                'type' => 1,
                                'create_time' => time(),
                            ];
                        }

                        $inserts = Db::table('fa_oa_relevance')
                            ->insertAll($arrs);
                    }else{
                        $inserts = 1;
                    }

                }

                if (($insert !== false) && ($inserts !== false)){
                    //事务提交
                    Db::commit();
                    $this->success('成功');
                }else{
                    //事务回滚
                    Db::rollback();
                    $this->error('失败');
                }

            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }



    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);

        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }




}
